<?php

/**
 * This is the model class for table "{{product}}".
 *
 * The followings are the available columns in table '{{product}}':
 * @property integer $pro_id
 * @property integer $pro_cid
 * @property integer $pro_uid
 * @property string $pro_name
 * @property integer $pro_pic_id
 * @property string $pro_price
 * @property integer $pro_recommend
 * @property integer $pro_left
 * @property integer $pro_sold
 * @property integer $pro_discount
 * @property integer $pro_create
 * @property integer $pro_update
 * @property string $pro_describe
 */
class Product extends CActiveRecord
{
	const COMMENT_ALLOW = 1;
	const COMMENT_CANCEL = 0;
	
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{product}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('pro_cid, pro_name, pro_price, pro_recommend, pro_left, pro_sold, pro_discount, pro_describe', 'required'),
			array('pro_cid, pro_uid, pro_pic_id, pro_recommend, pro_left, pro_sold, pro_discount', 'numerical', 'integerOnly'=>true),
			array('pro_price','numerical'),
			array('pro_name', 'length', 'max'=>200),
			array('pro_price', 'length', 'max'=>18),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('pro_id, pro_cid, pro_uid, pro_name, pro_pic_id, pro_price, pro_recommend, pro_left, pro_sold, pro_discount, pro_describe', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'file'	=> array(self::HAS_MANY, 'File', 'pro_id'),
			'owner'	=> array(self::BELONGS_TO, 'User', 'pro_uid')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'pro_id' => 'Product ID',
			'pro_cid' => 'Product Category',
			'pro_uid' => 'Product Owner',
			'pro_name' => 'Product Name',
			'pro_pic_id' => 'Product Picture',
			'pro_price' => 'Product Price',
			'pro_recommend' => 'Product Recommend',
			'pro_left' => 'Product Left',
			'pro_sold' => 'Product Sold',
			'pro_discount' => 'Product Discount',
			'pro_describe' => 'Product Describe',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('pro_id',$this->pro_id);
		$criteria->compare('pro_cid',$this->pro_cid);
		$criteria->compare('pro_uid',$this->pro_uid);
		$criteria->compare('pro_name',$this->pro_name,true);
		$criteria->compare('pro_pic_id',$this->pro_pic_id);
		$criteria->compare('pro_price',$this->pro_price,true);
		$criteria->compare('pro_recommend',$this->pro_recommend);
		$criteria->compare('pro_left',$this->pro_left);
		$criteria->compare('pro_sold',$this->pro_sold);
		$criteria->compare('pro_discount',$this->pro_discount);
		$criteria->compare('pro_describe',$this->pro_describe,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getCommentState($state)
	{
		switch ($state) {
			case self::COMMENT_ALLOW:
			 	return 'ALLOW';
				break;
			case self::COMMENT_CANCEL:
				return 'CANCEL';
			default:
				return 'ALLOW';
			break;
		}
	}
	
	public function generateCommentStateDropdownList()
	{
		return array(
			self::COMMENT_ALLOW=>self::getCommentState(self::COMMENT_ALLOW),
			self::COMMENT_CANCEL=>self::getCommentState(self::COMMENT_CANCEL)
		);
	}
	
	
	
	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->pro_create=$this->update_time=time();
				$this->pro_uid = Yii::app()->user->id;
			}
			else
				$this->pro_update=time();
			return true;
		}
		else
			return false;
	}
}